package logic

import (
	"context"
	"novel/model/mysql"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelListLogic {
	return &NovelListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelListLogic) NovelList(in *novelrpc.NovelListRequest) (*novelrpc.NovelListResponse, error) {
	// todo: add your logic here and delete this line

	var user []mysql.NovelUser

	mysql.DB.Debug().Model(&mysql.Novel{}).
		Select("novels.*, users.id as user_id").
		Joins("left join users on user_id = novels.user_id").
		Find(&user)

	var novel []*novelrpc.NovelList

	for _, m := range user {
		list := novelrpc.NovelList{
			Name:   m.Name,
			Types:  m.Types,
			Tile:   m.Tile,
			Ifor:   m.Ifor,
			UserId: int64(m.UserId),
		}
		novel = append(novel, &list)
	}

	return &novelrpc.NovelListResponse{
		List: novel,
	}, nil
}
